<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="form" :model="queryParams" :inline="true">
        <el-form-item prop="articleTitle" label="样本类型">
          <el-select v-model="queryParams.type" placeholder="请选择样本类型" @change="changeType">
            <el-option label="测序样本" value="PUBLICATION"></el-option>
            <el-option label="科研样本" value="SCIENTIFIC_RESEARCH"></el-option>
            <el-option label="报告样本" value="REPORT"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery" icon="el-icon-search">搜索</el-button>
          <el-button type="warning" @click="openSearchDialog()"><i class="el-icon-mouse"></i> 高级查询</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

    </div>

    <el-card shadow="never">
      <template #header>
        <el-button type="success" @click="openDialog()"
          ><i class="el-icon-circle-plus"></i> 新增</el-button>
        <el-button
          type="danger"
          :disabled="ids.length === 0"
          @click="handleDelete()"
          ><i class="el-icon-delete"></i> 删除</el-button>
      </template>

      <el-table
        v-loading="loading"
        :data="dataList"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" width="55" prop="dataIndex"/>
        <el-table-column label="样本类型" prop="typeText"/>
        <el-table-column label="样本名称" prop="name"/>
        <el-table-column label="测序方式" prop="layoutText"/>
        <el-table-column label="fastq1" prop="fastq1"/>
        <el-table-column label="fastq2" prop="fastq2"/>
        <el-table-column fixed="right" label="操作"  min-width="320" width="320">
          <template #default="scope">
            <el-button
              type="warning"
              size="small"
              link
              @click="openDialog(scope.row)"
            ><i class="el-icon-edit"></i> 编辑</el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="sendKafka(scope.row)" :disabled="scope.row.geneData"
            ><i class="el-icon-s-promotion"></i> 发送至Kafka</el-button>
            <el-button
              type="danger"
              size="small"
              link
              @click="handleDelete(scope.row)"
            ><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.pageNum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 文献表单弹窗 -->
    <el-dialog
      :visible.sync="dialog.visible"
      :title="dialog.title"
      width="900px"
      :before-close="closeDialog"
    >
      <el-form
        ref="seqDataFormRef"
        :model="formData"
        :rules="rules"
        label-width="150px"
      >
        <el-form-item label="样本类型" prop="name">
          <el-select v-model="formData.type" placeholder="请选择样本类型">
            <el-option label="测序样本" value="PUBLICATION"></el-option>
            <el-option label="科研样本" value="SCIENTIFIC_RESEARCH"></el-option>
            <el-option label="报告样本" value="REPORT"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="样本名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入样本名称" />
        </el-form-item>
        <el-form-item label="测序方式" prop="layout">
          <el-select v-model="formData.layout" placeholder="请选择测序方式">
            <el-option label="单端测序" value="SINGLE"></el-option>
            <el-option label="双端测序" value="PAIRED"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="fastq1" prop="fastq1">
          <el-input v-model="formData.fastq1" placeholder="请输入fastq1" />
        </el-form-item>
        <el-form-item label="fastq2" prop="fastq2" v-show="formData.layout == 'PAIRED'">
          <el-input v-model="formData.fastq2" placeholder="请输入fastq2" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="addOrUpdate">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      :visible.sync="searchDialog"
      title="高级查询"
      width="500px"
      :before-close="closeSearchDialog"
    >
      <el-form
        ref="searchFormRef"
        :model="formData"
        :rules="rules"
        label-width="0px"
      >
        <el-form-item v-for="(item, index) in searchFormData" :key="index" label-width="0">
          <el-col :span="7">
            <el-select v-model="searchFormData[index].name">
              <el-option v-for="field in fieldList" :key="field.value" :label="field.label" :value="field.value"></el-option>
            </el-select>
          </el-col>
          <el-col :span="1">&nbsp;</el-col>
          <el-col :span="4">
            <el-select v-model="searchFormData[index].searchType">
              <el-option v-for="cont in contList" :key="cont.value" :label="cont.label" :value="cont.value"></el-option>
            </el-select>
          </el-col>
          <el-col :span="1">&nbsp;</el-col>
          <el-col :span="7">
            <el-input v-model="searchFormData[index].searchVal" placeholder="请输入查询内容"></el-input>
          </el-col>
          <el-col class="line" :span="4"><i class="el-icon-remove-outline" style="font-size: 22px; cursor: pointer; margin-left: 5px;" @click="delSearchFormData(index)"></i></el-col>
        </el-form-item>
        
      </el-form>
      <div style="margin-top: 10px;">
        <i class="el-icon-circle-plus-outline" style="font-size: 22px; cursor: pointer;" @click="addSearchFormData"></i>
        
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="search">查 询</el-button>
          <el-button @click="closeSearchDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import seqSampleApi from '@/api/sequence/seqSample'
import { formatDate } from '@/utils/formatTime'
import qs from 'qs';
export default {
	name: 'seqDataIndex',
	data() {
		return {
			queryParams: {
        type: 'PUBLICATION',
        pageNum: 0,
        pageSize: 10,

      },
      total: 0,
      loading: false,
      dataList: [],
      ids: [],
      dialog: {},
      formData: {},
      rules: {},

      fieldList: [{label: '样本名称', value: 'name'}, {label: '测序方式', value: 'layout'}, {label: 'fastq1', value: 'fastq1'}, {label: 'fastq2', value: 'fastq2'}],
      contList: [{label: 'LIKE', value: 'LIKE'}, {label: '等于', value: 'EQUAL'}, {label: '大于', value: 'GT'}, {label: '大于等于', value: 'GTE'}
        , {label: '小于', value: 'LT'}, {label: '小于等于', value: 'LTE'}],
      searchDialog: false,
      searchFormData: [],
      typeMap: {'PUBLICATION': '测序样本', 'SCIENTIFIC_RESEARCH': '科研样本', 'REPORT': '报告样本'},
		};
	},
  methods: {
    changeType () {
      this.handleQuery();
    },
    addSearchFormData () {
      this.searchFormData.push({})
    },
    delSearchFormData (index) {
      this.searchFormData.splice(index, 1);
    },
    search () {
      this.handleQuery()
      this.searchDialog = false;
    },
    openSearchDialog () {
      this.searchDialog = true;
    },
    closeSearchDialog () {
      this.searchDialog = false;
    },
    sendKafka (row) {
      seqSampleApi.sendKafka(row.id, row.type).then(res =>{
        this.$message({message: '测序样本已发送至Kafka！', type: 'success'});
        this.handleQuery();
      })
    },
    handleQuery() {
      this.loading = true;
      let params = {
        number: this.queryParams.pageNum,
        size: this.queryParams.pageSize,
        criteriaVos: this.searchFormData,
      };
      seqSampleApi.page(this.queryParams.type, params).then((res) => {
        let data = res.data;
        let that = this;
        let index = 1
        data.content.forEach((v) => {
          v.dataIndex = index++;
          v.layoutText = v.layout ? (v.layout === 'PAIRED' ? '双端测序' : '单端测序') : '-';
          v.typeText = that.typeMap[v.type];
        });
        this.dataList = data.content;
        
        this.total = data.count;
        this.loading = false;
      }).catch(() => {});
    },
    resetQuery() {
      this.queryParams = {
        type: 'PUBLICATION',
        pageNum: 0,
        pageSize: 10,
      };
      this.searchFormData = [];
      this.handleQuery();
    },
    
    addOrUpdate() {
      let that = this;
      if (this.formData.id) {
        seqSampleApi.update(this.formData).then((res) => {
          this.$message({message: '样本更新成功！', type: 'success'});
          that.closeDialog();
        });
      } else {
        seqSampleApi.add(this.formData).then((res) => {
          this.$message({message: '样本添加成功！', type: 'success'});
          that.closeDialog();
        });
      }
    },
    openDialog(row) {
      this.$set(this.dialog, 'visible', true);
      let that = this;
      if (row) {
        this.$set(this.dialog, 'title', '编辑样本信息');
        seqSampleApi.get(row.id, row.type).then((res) => {
          let data = res.data;
          that.formData = data;

        });
      } else {
        this.$set(this.dialog, 'title', '新增样本信息');
        that.formData = {};
      }

    },
    closeDialog() {
      this.$set(this.dialog, 'visible', false);
      this.$set(this.dialog, 'title', '');
      this.handleQuery();
    },
    handleDelete(row) {
      if (row) {
        seqSampleApi.delete(row.id, row.type).then((res) => {
          this.$message({message: '样本删除成功！', type: 'success'});
          this.handleQuery();
        });
      } else {
        this.$message.error('暂不支持批量删除样本信息！');
      }
    },
    handleSelectionChange(val) {
      this.ids = [];
      val.forEach(v => {
        this.ids.push(v.id);
      });
    },
    handleSelectionRoleChange(rows) {
      if (rows) {
        let arr = [];
        rows.forEach((v) => {
          arr.push(v.id);
        });
        this.selectRoleIdArr = arr;
      }
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.handleQuery();
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.handleQuery();
    },
  },
  created() {
    this.handleQuery();
  }
};
</script>

<style scoped>
.el-pagination {
  margin-top: 10px;
  float: right;
  margin-bottom: 10px;
}
</style>